How can I use `pipe` to facilitate interprocess communication in Perl?

Posted by Shiftbit on Stack Overflow See other posts from Stack Overflow or by Shiftbit
Published on 2010-04-01T04:21:09Z Indexed on 2010/04/01 4:43 UTC
Read the original article Hit count: 304

Filed under:
|

Can anyone explain how I can successfully get my processes communicating? I find the perldoc on IPC confusing.

What I have so far is:

$| = 1;
$SIG{CHLD} = {wait};
my $parentPid = $$;

if ($pid = fork();) ) {
    if ($pid == 0) {
       pipe($parentPid, $$);
       open PARENT, "<$parentPid";
       while (<PARENT>) {
           print $_;
       }
       close PARENT;
       exit();
    } else {

       pipe($parentPid, $pid);
       open CHILD, ">$pid";
          or error("\nError opening: childPid\nRef: $!\n");
       open (FH, "<list")
          or error("\nError opening: list\nRef: $!\n");

       while(<FH>) {
          print CHILD, $_;
       }

       close FH
          or error("\nError closing: list\nRef: $!\n");
       close CHILD
          or error("\nError closing: childPid\nRef: $!\n);
    } else {
    error("\nError forking\nRef: $!\n");
}
  • First: What does perldoc pipe mean by READHANDLE, WRITEHANDLE?

  • Second: Can I implement a solution without relying on CPAN or other modules?

© Stack Overflow or respective owner

Related posts about perl

Related posts about ipc